import { ecs } from "db://oops-framework/libs/ecs/ECS";
import { MapModelComp } from "db://assets/script/game/battle/scene/model/MapModelComp";
import { MapLoadComp } from "db://assets/script/game/battle/scene/bll/MapLoad";
import { MapViewComp } from "db://assets/script/game/battle/scene/view/MapViewComp";
import { LoadBattleDataComp } from "db://assets/script/game/battle/scene/bll/LoadBattleData";
import { MapModelRoleComp } from "db://assets/script/game/battle/scene/model/MapModelRoleComp";
import { LoadBattleRolesComp } from "db://assets/script/game/battle/scene/bll/LoadBattleRoles";
import { BattleModelComp } from "db://assets/script/game/battle/scene/model/BattleModelComp";
import { BattleStartComp } from "db://assets/script/game/battle/scene/bll/BattleStart";
import { BattleAnimatorComp } from "./bll/BattleAnimator";
import { RoleAttributeType } from "../../role/model/RoleEnum";
import { RoundStartComp } from "./bll/RoundStart";
import { BattleAutoComp } from "./bll/BattleAuto";
import { RoundEndComp } from "./bll/RoundEnd";
import { BattleEndComp } from "./bll/BattleEnd";

/** 游戏地图 */
@ecs.register(`Scene`)
export class Scene extends ecs.Entity {
    //数据层
    MapModel!: MapModelComp;
    MapModelRole!: MapModelRoleComp;
    BattleModel!: BattleModelComp;
    //逻辑层
    LoadBattleData!: LoadBattleDataComp;
    LoadBattleRoles!: LoadBattleRolesComp;
    BattleStart!: BattleStartComp;
    BattleEnd!: BattleEndComp;
    //视图层
    MapView!: MapViewComp;

    protected init(): void {
        this.addComponents<ecs.Comp>(
            MapModelComp,
            MapModelRoleComp,
            BattleModelComp
        );
        this.BattleModel.vmAdd();
    }
    destroy(): void {
        this.remove(BattleStartComp);
        this.remove(MapViewComp);
        this.remove(LoadBattleDataComp);
        this.remove(LoadBattleRolesComp);

        this.BattleModel.vmRemove();

        super.destroy();
    }
    /**
     * 加载并显示地图
     * @param id 地图编号
     */
    load(id: number) {
        this.MapModel.id = id;
        this.add(MapLoadComp);
    }
    /** 播放战场动画 */
    play(onComplete: Function) {
        if (this.has(BattleAnimatorComp)) {
            this.remove(BattleAnimatorComp)
        }
        this.add(BattleAnimatorComp).onComplete = onComplete;
    }
    /** 战斗前加载数据 1: 人跟怪，2: 人， 3：怪 */
    loadBattleData(loadType: number) {
        this.add(LoadBattleDataComp).loadType = loadType;
    }
    /** 加载战斗人物模型 */
    loadBattleRoles(loadType: number) {
        this.add(LoadBattleRolesComp).loadType = loadType;
    }

    /** boss或者宝箱怪倒计时 */
    setBossLastTime(time: number, bossName: string) {
        // var bltc = this.add(BossLastTimeComp);
        // bltc.time = time;
        // bltc.bossName = bossName;
    }

    /** 设置怪物总血量 1:战斗开始，2:战斗中 */
    setEnemysTotalHp(type: number) {
        // if (this.has(SetEnemysTotalHpComp)) {
        //     this.remove(SetEnemysTotalHpComp);
        // }
        // this.add(SetEnemysTotalHpComp).type = type;
    }
    /**战斗开始 */
    battleStart() {
        if (this.has(BattleStartComp)) {
            this.remove(BattleStartComp);
        }
        this.add(BattleStartComp);
    }

    /**回合开始 */
    roundStart() {
        this.add(RoundStartComp);
    }
    /**开始自动战斗 */
    autoStart() {
        if (this.has(BattleAutoComp)) {
            this.remove(BattleAutoComp);
        }
        this.add(BattleAutoComp);
    }
    /**回合结束 */
    roundEnd() {
        this.add(RoundEndComp);
    }

    /**战斗结束 */
    battleEnd(isWin: boolean) {
        this.add(BattleEndComp).isWin = isWin;
    }
    /**显示主线通关失败页面 */
    loadPassFailedPage() {
        // var callbacks: UICallbacks = {
        //     onAdded: (node: Node, params: any) => {
        //         this.add(node.getComponent(PassFailedPageViewComp) as ecs.Comp);
        //     }
        // };
        // oops.gui.open(UIID.PassFailedPage, null, callbacks);
        console.warn(`###显示主线通关失败页面### todo`);
    }
    /**
     * 将所有角色按基础攻速进行排序
     * 攻速一致按pos排序;
     * @param rat 排序属性默认攻速;
     * @param rmt 属性模块默认基础属性;
     * @returns Role[];
     */
    sortBySpd(rat: RoleAttributeType = RoleAttributeType.spd) {
        this.MapModelRole.roles.sort((role1, role2) => {
            let role1_spd = role1.getNumericValue(rat);
            let role2_spd = role2.getNumericValue(rat);

            if (role1_spd == role2_spd) {
                role1_spd = role1.pos;
                role2_spd = role2.pos;
            }

            return role2_spd - role1_spd;
        })
    }
}